/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.biz.domain.Lock;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.LockRepository;
import me.zhengjie.modules.biz.service.LockService;
import me.zhengjie.modules.biz.service.dto.LockDto;
import me.zhengjie.modules.biz.service.dto.LockQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.LockMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-04-18
**/
@Service
@RequiredArgsConstructor
public class LockServiceImpl implements LockService {

    private final LockRepository lockRepository;
    private final LockMapper lockMapper;

    @Override
    public Map<String,Object> queryAll(LockQueryCriteria criteria, Pageable pageable){
        Page<Lock> page = lockRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(lockMapper::toDto));
    }

    @Override
    public List<LockDto> queryAll(LockQueryCriteria criteria){
        return lockMapper.toDto(lockRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LockDto findById(Integer cid) {
        Lock lock = lockRepository.findById(cid).orElseGet(Lock::new);
        ValidationUtil.isNull(lock.getCid(),"Lock","cid",cid);
        return lockMapper.toDto(lock);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LockDto create(Lock resources) {
//        if(lockRepository.findById(resources.getId()) != null){
//            throw new EntityExistException(Lock.class,"id",resources.getId());
//        }
        return lockMapper.toDto(lockRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Lock resources) {
        Lock lock = lockRepository.findById(resources.getCid()).orElseGet(Lock::new);
        ValidationUtil.isNull( lock.getCid(),"Lock","id",resources.getCid());
//        lock1 = lockRepository.findById(resources.getId());
//        if(lock1 != null && !lock1.getCid().equals(lock.getCid())){
//            throw new EntityExistException(Lock.class,"id",resources.getId());
//        }
        lock.copy(resources);
        lockRepository.save(lock);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer cid : ids) {
            lockRepository.deleteById(cid);
        }
    }

    @Override
    public void download(List<LockDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LockDto lock : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" id",  lock.getId());
            map.put(" type",  lock.getType());
            map.put(" isOpen",  lock.getIsOpen());
            map.put(" mac",  lock.getMac());
            map.put(" createdAt",  lock.getCreatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
